package day22.HomeWork;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

interface Terrestrial {

    int getLegNum();

}

public abstract class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public abstract void shout();
}

class Cat extends Animal implements Terrestrial {
    private Integer legNum;

    public void setLegNum(Integer legNum) {
        this.legNum = legNum;
    }

    public Cat(String name, Integer legNum) {
        super(name);
        this.legNum = legNum;
    }

    @Override
    public void shout() {
        System.out.println("喵喵喵……");
    }

    @Override
    public int getLegNum() {
        return legNum;
    }

    @Override
    public String toString() {
        return getName() + '\t' + getLegNum() + '\t';
    }
}

class Duck extends Animal implements Terrestrial {
    private int legNum;

    public void setLegNum(int legNum) {
        this.legNum = legNum;
    }

    public Duck(String name, int legNum) {
        super(name);
        this.legNum = legNum;
    }

    @Override
    public void shout() {
        System.out.println("嘎嘎嘎……");
    }

    @Override
    public int getLegNum() {
        return legNum;
    }

    public String toString() {
        return getName() + '\t' + getLegNum() + '\t';
    }
}

class Dolphin extends Animal {
    public Dolphin(String name) {
        super(name);
    }

    @Override
    public void shout() {
        System.out.println("海豚音……");
    }

    public String toString() {
        return getName() + '\t' + 0 + '\t';
    }

}

class Main {
    public static void modify() throws Exception {
        throw new Exception("输入腿的数量有错");

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Animal> list = new ArrayList<>();
        list.add(new Cat("加菲猫", 4));
        list.add(new Duck("唐小鸭", 2));
        list.add(new Dolphin("小海豚"));
        System.out.println("动物名字" + '\t' + "腿的条数" + '\t' + "动物叫");
        for (Animal animal : list) {
            System.out.print(animal);
            System.out.print('\t');
            animal.shout();
        }
        while (true) {
            System.out.println("是否要继续修改数据：按o进行修改操作，其他任意数字键退出");
            String str = sc.next();
            if (str.equals("o")) {
                System.out.println("请输入猫的名称和腿的条数");
                String s = sc.next();
                int num = sc.nextInt();
               boolean flag=false;
                for (Animal animal : list) {
                     flag= s.equals(animal.getName());
                    if (flag) {
                        if (animal instanceof Cat) {
                            try {
                                if(num>4){
                                    modify();
                                }else{
                                    ((Cat) animal).setLegNum(num);
                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        } else if (animal instanceof Duck) {
                            try {
                                if(num>2){
                                    modify();
                                }else{
                                    ((Cat) animal).setLegNum(num);
                                }

                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            System.out.println("yazi");
                        } else if (animal instanceof Dolphin) {
                            try {
                                if(num!=0){
                                    modify();
                                }

                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        break;
                    }
                }
                if (flag==false) {
                    System.out.println("输入的名字不匹配，请重新输入");
                }
                for (Animal animal : list) {
                    System.out.print(animal);
                    System.out.print('\t');
                    animal.shout();
                }
            } else {
                //按任意键退出
                System.out.println("系统已退出");
                break;
            }

        }

    }
}